package bomberman;

import java.awt.Point;
import java.awt.event.KeyEvent;

import bomberman.entity.Bomberman;



import gameframework.base.MoveStrategyKeyboard;
import gameframework.base.SpeedVector;
import gameframework.base.SpeedVectorDefaultImpl;

public class MoveStrategyKeyboardPlayer extends MoveStrategyKeyboard {
	SpeedVector tmpSpeedV;
	Boolean move = false;
	
	private Bomberman player;
	
	int right, left, up, down, bomb;
	
	public MoveStrategyKeyboardPlayer(Bomberman bm, int right, int left, int up, int down, int bomb) {
		player = bm;
		this.right = right;
		this.left = left;
		this.up = up;
		this.down = down;
		this.bomb = bomb;
		tmpSpeedV = new SpeedVectorDefaultImpl(new Point(0,0));
	}
	
	@Override
	public void keyPressed(KeyEvent event) {
		//The player can move in a direction only if he is not
		//between 2 squares
		if(player.getPosition().x%BombermanGameLevel.SPRITE_SIZE == 0 
				&& player.getPosition().y%BombermanGameLevel.SPRITE_SIZE == 0)
		{
			int keycode = event.getKeyCode();
			if (keycode == right)
			{
				move = true;
				speedVector.setDirection(new Point(1, 0));
			}
			
			if(keycode == left)
			{
				move = true;
				speedVector.setDirection(new Point(-1, 0));
			}
			
			if(keycode == up)
			{
				move = true;
				speedVector.setDirection(new Point(0, -1));
			}
			
			if(keycode == down)
			{
				move = true;
				speedVector.setDirection(new Point(0, 1));
			}
			
			tmpSpeedV = speedVector;
			
			if(keycode == bomb)
				player.dropBomb();
		}
	}
	
	public void keyReleased(KeyEvent e) {
		if ((e.getKeyCode() == right || e.getKeyCode() == left
				|| e.getKeyCode() == up
				|| e.getKeyCode() == down) ) {
			move = false;
			speedVector = getSpeedVector();
		}
		
	}
	
	public SpeedVector getSpeedVector(){
		//If the player stop, we check if he is on a square
		if(!move)
		{
			if(player.getPosition().x%BombermanGameLevel.SPRITE_SIZE != 0 
					|| player.getPosition().y%BombermanGameLevel.SPRITE_SIZE != 0)
				speedVector.setDirection(tmpSpeedV.getDirection());
			else
				speedVector.setDirection(new Point(0,0));
		}
		return speedVector;
	}
}
